#include "stdafx.h"
#include "wf_dimlinear.h"

#include "pf_constructionline.h"

/**
 * Constructor.
 *
 * @param angle Initial angle in rad.
 * @param fixedAngle true: The user can't change the angle.
 *                   false: The user can change the angle in a option widget.
 */
WF_DimLinear::WF_DimLinear(PF_Container& container,
                                       PF_GraphicView& graphicView,
                                       double angle,
                                       bool fixedAngle)
        :WF_Dimension("Draw linear dimensions",
                    container, graphicView) {

    edata.angle = angle;
    this->fixedAngle = fixedAngle;

    lastStatus = SetExtPoint1;

    reset();
}



WF_DimLinear::~WF_DimLinear() {}


QAction* WF_DimLinear::createGUIAction(LX_Define::ActionType type, QObject* /*parent*/) {
    QAction* action;

    switch (type) {
    default:
    case LX_Define::ActionDimLinear:
		// tr("Linear")
		action = new QAction(tr("&Linear"),  NULL);
                action->setIcon(QIcon(":/extui/dimlinear.png"));
        //action->zetStatusTip(tr("Linear Dimension"));
        break;

    case LX_Define::ActionDimLinearHor:
		// tr("Horizontal")
		action = new QAction(tr("&Horizontal"), NULL);
		action->setIcon(QIcon(":/extui/dimhor.png"));
        //action->zetStatusTip(tr("Horizontal Dimension"));
        break;

    case LX_Define::ActionDimLinearVer:
		// tr("Vertical")
		action = new QAction(tr("&Vertical"), NULL);
		action->setIcon(QIcon(":/extui/dimver.png"));
        //action->zetStatusTip(tr("Vertical Dimension"));
        break;
    }

    return action;
}


void WF_DimLinear::reset() {
    WF_Dimension::reset();

    edata = PF_DimLinearAttribute(PF_Vector(false),
                             PF_Vector(false),
                             (fixedAngle ? edata.angle : 0.0), 0.0);

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true, true);
    }
}



void WF_DimLinear::trigger() {
    WF_Dimension::trigger();

    preparePreview();
    PF_DimLinear* dim = new PF_DimLinear(container, data, edata);
    dim->setLayerToActive();
    dim->setPenToActive();
    dim->update();
    container->addEntity(dim);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(dim);
        document->endUndoCycle();
    }

    PF_Vector rz = graphicView->getRelativeZero();
	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(rz);

    PF_DEBUG->print("PF_ActionDimLinear::trigger():"
                    " dim added: %d", dim->getId());
}


void WF_DimLinear::preparePreview() {
    PF_Vector dirV;
    dirV.setPolar(100.0, edata.angle+M_PI/2.0);

    PF_ConstructionLine cl(
        NULL,
        PF_ConstructionLineAttribute(
            edata.extensionPoint2,
            edata.extensionPoint2+dirV));

    data.definitionPoint =
        cl.getNearestPointOnEntity(data.definitionPoint);

}



void WF_DimLinear::mouseMoveEvent(QMouseEvent* e) {
    PF_DEBUG->print("PF_ActionDimLinear::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);

    switch (getStatus()) {
    case SetExtPoint1:
        break;

    case SetExtPoint2:
        if (edata.extensionPoint1.valid) {
            deletePreview();
            preview->addEntity(new PF_Line(preview,
                                           PF_LineAttribute(edata.extensionPoint1,
                                                       mouse)));
            drawPreview();
        }
        break;

    case SetDefPoint:
        if (edata.extensionPoint1.valid && edata.extensionPoint2.valid) {
            deletePreview();
            data.definitionPoint = mouse;

            preparePreview();

            PF_DimLinear* dim = new PF_DimLinear(preview, data, edata);
            dim->update();
            preview->addEntity(dim);
            drawPreview();
        }
        break;
    }

    PF_DEBUG->print("PF_ActionDimLinear::mouseMoveEvent end");
}



void WF_DimLinear::mouseReleaseEvent(QMouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DimLinear::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector pos = e->getCoordinate();

    switch (getStatus()) {
    case SetExtPoint1:
        edata.extensionPoint1 = pos;
        graphicView->moveRelativeZero(pos);
        setStatus(SetExtPoint2);
        break;

    case SetExtPoint2:
        edata.extensionPoint2 = pos;
        graphicView->moveRelativeZero(pos);
        setStatus(SetDefPoint);
        break;

    case SetDefPoint:
        data.definitionPoint = pos;
        trigger();
        reset();
        setStatus(SetExtPoint1);
        break;

    default:
        break;
    }
}



void WF_DimLinear::commandEvent(PF_CommandEvent* e) {
    QString c = e->getCommand().toLower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {
    case SetText:
        setText(c);
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->requestOptions(this, true, true);
        }
        graphicView->enableCoordinateInput();
        setStatus(lastStatus);
        break;

    case SetAngle: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                setAngle(PF_Math::deg2rad(a));
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
            if (PF_DIALOGFACTORY!=NULL) {
                PF_DIALOGFACTORY->requestOptions(this, true, true);
            }
            setStatus(lastStatus);
        }
        break;

    default:
        lastStatus = (Status)getStatus();
        deletePreview();
        if (checkCommand("text", c)) {
            graphicView->disableCoordinateInput();
            setStatus(SetText);
            return;
        } else if (!fixedAngle && (checkCommand("angle", c))) {
            setStatus(SetAngle);
        }
        break;
    }
}



QStringList WF_DimLinear::getAvailableCommands() {
    QStringList cmd;

    switch (getStatus()) {
    case SetExtPoint1:
    case SetExtPoint2:
    case SetDefPoint:
        cmd += command("text");
        if (!fixedAngle) {
            cmd += command("angle");
        }
        break;

    default:
        break;
    }

    return cmd;
}


void WF_DimLinear::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetExtPoint1:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify first extension line origin"),
                tr("Cancel"));
            break;
        case SetExtPoint2:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify second extension line origin"),
                tr("Back"));
            break;
        case SetDefPoint:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify dimension line location"),
                tr("Back"));
            break;
        case SetText:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
            break;
        case SetAngle:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Enter dimension line angle:"), "");
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DimLinear::showOptions() {
    PF_ActionInterface::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true, true);
    }
}



void WF_DimLinear::hideOptions() {
    PF_ActionInterface::hideOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }
}



// EOF
